package com.stars.services.impl.job;
import com.stars.common.WheterYN;
import com.stars.common.base.BaseConstant;
import com.stars.common.enums.JobOperateEnum;
import com.stars.entity.job.MyScheduleJobEntity;
import com.stars.services.MyQuartzService;
import com.stars.services.job.MyScheduleJobAccessServices;
import com.stars.services.job.MyScheduleJobConfigServices;
import com.stars.dao.mapper.job.MyScheduleJobMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.stars.services.impl.BaseConfigServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Title: 任务调度 业务逻辑
 * @author dj
 * @date 2019-10-12 13:54:31
 * @version V1.0
 *
 */
@Service
public class MyScheduleJobConfigServicesImpl  extends BaseConfigServiceImpl<MyScheduleJobMapper , MyScheduleJobEntity> implements MyScheduleJobConfigServices {

    @Autowired
    private MyQuartzService myQuartzService;
    @Autowired
    private MyScheduleJobAccessServices myScheduleJobAccessServicesImpl;

    @Override
    public void add(MyScheduleJobEntity job) throws Exception{
        this.insertSelective(job);
        myQuartzService.addJob(job);
    }

    @Override
    public void addJob(String[] ids) throws Exception {
        for (String id : ids) {
            if(StringUtils.isBlank(id)){
                continue;
            }
            MyScheduleJobEntity myScheduleJobEntity = myScheduleJobAccessServicesImpl.selectById(id);
            if(null == myScheduleJobEntity){
                continue;
            }
            myQuartzService.addJob(myScheduleJobEntity);
        }
    }

    @Override
    public void start(MyScheduleJobEntity job)throws Exception {
        MyScheduleJobEntity myScheduleJobEntity = myScheduleJobAccessServicesImpl.selectById(job.getId());
        myScheduleJobEntity.setStatusJob(JobOperateEnum.START.getValue().toString());
        myScheduleJobEntity.setStatusDate(new Date());
        myScheduleJobEntity.setIsNowRun(WheterYN.YES.getValue());
        this.updateSelectiveById(myScheduleJobEntity);
        //执行job
        myQuartzService.operateJob(JobOperateEnum.START, myScheduleJobEntity);
    }

    @Override
    public void pause(MyScheduleJobEntity job) throws Exception{
        MyScheduleJobEntity myScheduleJobEntity = myScheduleJobAccessServicesImpl.selectById(job.getId());
        myScheduleJobEntity.setStatusJob(JobOperateEnum.PAUSE.getValue().toString());
        myScheduleJobEntity.setEndDate(new Date());
        myScheduleJobEntity.setIsNowRun(WheterYN.NO.getValue());
        this.updateById(myScheduleJobEntity);
        //执行job
        myQuartzService.operateJob(JobOperateEnum.PAUSE, myScheduleJobEntity);
    }

    @Override
    public void deleteById(MyScheduleJobEntity job) throws Exception{
        MyScheduleJobEntity myScheduleJobEntity = myScheduleJobAccessServicesImpl.selectById(job.getId());
        myScheduleJobEntity.setStatusJob(JobOperateEnum.DELETE.getValue().toString());
        this.updateSelectiveById(myScheduleJobEntity);
        //执行job
        myQuartzService.operateJob(JobOperateEnum.DELETE, myScheduleJobEntity);
    }

    @Override
    public void startAllJob()throws Exception {
        List<MyScheduleJobEntity> myScheduleJobEntities = myScheduleJobAccessServicesImpl.selectListAll();
        for (MyScheduleJobEntity myScheduleJobEntity : myScheduleJobEntities) {
            myScheduleJobEntity.setStatusJob(JobOperateEnum.START.getValue().toString());
            myScheduleJobEntity.setStatusDate(new Date());
            myScheduleJobEntity.setIsNowRun(WheterYN.YES.getValue());
            this.updateSelectiveById(myScheduleJobEntity);
        }
        //执行job
        myQuartzService.startAllJob();

    }

    @Override
    public void pauseAllJob() throws Exception{
        List<MyScheduleJobEntity> myScheduleJobEntities = myScheduleJobAccessServicesImpl.selectListAll();
        for (MyScheduleJobEntity myScheduleJobEntity : myScheduleJobEntities) {
            myScheduleJobEntity.setStatusJob(JobOperateEnum.PAUSE.getValue().toString());
            myScheduleJobEntity.setEndDate(new Date());
            myScheduleJobEntity.setIsNowRun(WheterYN.NO.getValue());
            this.updateById(myScheduleJobEntity);
        }
        //执行job
        myQuartzService.pauseAllJob();
    }
}